package com.echat.messagesender.sender.service.impl;

import com.echat.messagesender.sender.mapper.EmailTemplateMapper;
import com.echat.messagesender.sender.mapper.EmailTemplateMappingMapper;
import com.echat.messagesender.sender.mapper.SmsTemplateMapper;
import com.echat.messagesender.sender.mapper.SmsTemplateMappingMapper;
import com.echat.messagesender.sender.model.EmailTemplate;
import com.echat.messagesender.sender.model.EmailTemplateMapping;
import com.echat.messagesender.sender.model.SmsTemplate;
import com.echat.messagesender.sender.model.SmsTemplateMapping;
import com.echat.messagesender.sender.service.TemplateService;
import com.echat.messagesender.sender.util.IdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by chenmohan on 2018/09/10
 *
 * @description
 */
@Service
public class TemplateServiceImpl implements TemplateService {

    @Autowired
    EmailTemplateMapper emailTemplateMapper;

    @Autowired
    EmailTemplateMappingMapper emailTemplateMappingMapper;

    @Autowired
    SmsTemplateMapper smsTemplateMapper;

    @Autowired
    SmsTemplateMappingMapper smsTemplateMappingMapper;

    @Autowired
    IdGenerator idGenerator;

    @Override
    public long addEmailTemplate(EmailTemplate et) {
        //校验
        et.check();
        //do
        long templateId = idGenerator.genId();
        LocalDateTime nowTime = LocalDateTime.now();
        et.setId(templateId);
        et.setName(et.getName());
        et.setCreateTime(nowTime);
        et.setModifyTime(nowTime);
        emailTemplateMapper.insertSelective(et);
        return templateId;

    }

    @Override
    public long addSmsTemplate(SmsTemplate st) {
        //校验
        st.check();
        //do
        long templateId = idGenerator.genId();
        LocalDateTime nowTime = LocalDateTime.now();
        st.setId(templateId);
        st.setCreateTime(nowTime);
        st.setModifyTime(nowTime);
        smsTemplateMapper.insertSelective(st);
        return templateId;
    }

    @Override
    public List<EmailTemplate> listAllEmailTemplates() {
        return emailTemplateMapper.selectAll();
    }

    @Override
    public List<SmsTemplate> listAllSmsTemplates() {
        return smsTemplateMapper.selectAll();
    }

    @Override
    public EmailTemplate getEmailTemplateById(Long templateId) {
        return emailTemplateMapper.selectByPrimaryKey(templateId);
    }

    @Override
    public SmsTemplate getSmsTemplateById(Long templateId) {
        Example example = new Example(SmsTemplate.class);
        example.createCriteria().andEqualTo("id", templateId);
        return smsTemplateMapper.selectOneByExample(example);
    }

    @Override
    public void addEmailTemplateMapping(EmailTemplateMapping mapping) {
        //校验
        mapping.check();

        LocalDateTime nowTime = LocalDateTime.now();
        mapping.setCreateTime(nowTime);
        mapping.setModifyTime(nowTime);
        emailTemplateMappingMapper.insertSelective(mapping);
    }

    @Override
    public void addSmsTemplateMapping(SmsTemplateMapping mapping) {
        //校验
        mapping.check();

        LocalDateTime nowTime = LocalDateTime.now();
        mapping.setCreateTime(nowTime);
        mapping.setModifyTime(nowTime);
        smsTemplateMappingMapper.insertSelective(mapping);
    }

    @Override
    public List<EmailTemplateMapping> getEmailTemplateMappingByTemplateId(Long templateId) {
        if (templateId == null) {
            throw new IllegalArgumentException("templateId 不能为空");
        }
        EmailTemplateMapping query = EmailTemplateMapping.builder().templateId(templateId).build();
        List<EmailTemplateMapping> resultList = emailTemplateMappingMapper.select(query);
        return resultList;
    }

    @Override
    public List<SmsTemplateMapping> getSmsTemplateMappingByTemplateId(Long templateId) {
        if (templateId == null) {
            throw new IllegalArgumentException("templateId 不能为空");
        }
        SmsTemplateMapping query = SmsTemplateMapping.builder().templateId(templateId).build();
        List<SmsTemplateMapping> resultList = smsTemplateMappingMapper.select(query);
        return resultList;
    }


    @Override
    public EmailTemplateMapping getEmailTemplateMappingId(Long templateId, Long platformId) {
        Map<Long, EmailTemplateMapping> mappingMap = getEmailTemplateMappingByTemplateId(templateId).parallelStream()
                .collect(Collectors.toMap(e -> e.getPlatformId(), e -> e));
        return getEmailMappingTemplateId(platformId, mappingMap);
    }

    @Override
    public SmsTemplateMapping getSmsTemplateMappingId(Long templateId, Long platformId) {
        Map<Long, SmsTemplateMapping> mappingMap = getSmsTemplateMappingByTemplateId(templateId).parallelStream()
                .collect(Collectors.toMap(e -> e.getPlatformId(), e -> e));
        return getSmsMappingTemplateId(templateId, platformId, mappingMap);
    }

    /**
     * getEmailTemplateMappingId(...)和getSmsTemplateMappingId(...)会调用此方法
     * 获取相应平台的模板id
     *
     * @param platformId      平台id
     * @param mappingMap      模板id和平台的模板id映射map
     * @return
     */
    private EmailTemplateMapping getEmailMappingTemplateId( Long platformId, Map<Long, EmailTemplateMapping> mappingMap) {
        EmailTemplateMapping mapping = mappingMap.get(platformId);
        return mapping;
    }

    /**
     * getEmailTemplateMappingId(...)和getSmsTemplateMappingId(...)会调用此方法
     * 获取相应平台的模板id
     *
     * @param orginTemplateId 在message-sender应用中使用的虚拟模板id
     * @param platformId      平台id
     * @param mappingMap      模板id和平台的模板id映射map
     * @return
     */
    private SmsTemplateMapping getSmsMappingTemplateId(Long orginTemplateId, Long platformId, Map<Long, SmsTemplateMapping> mappingMap) {
        SmsTemplateMapping mapping = mappingMap.get(platformId);
        return mapping;
    }
}
